/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.TimKiem;

import BUS.MonAn_BUS;
import java.awt.HeadlessException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mr
 */
public class JIFTimMonAn extends javax.swing.JInternalFrame {
    DefaultTableModel tbModelMA;
    /**
     * Creates new form JIFTimMonAn
     */
    public JIFTimMonAn() throws Exception {
        initComponents();
        this.setSize(880, 560);
        tbModelMA = MonAn_BUS.layBangMonAn();
        tbMonAn.setModel(tbModelMA);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbMonAn = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        btTimMA = new javax.swing.JButton();
        tfDen = new javax.swing.JTextField();
        tfTu = new javax.swing.JTextField();
        tfTenMonAn = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("TÌM KIẾM MÓN ĂN");

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        tbMonAn.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbMonAn);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        btTimMA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/find.png"))); // NOI18N
        btTimMA.setText("Tìm Theo Giá");
        btTimMA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTimMAActionPerformed(evt);
            }
        });

        tfDen.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfDen.setText("0");

        tfTu.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfTu.setText("0");

        tfTenMonAn.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                tfTenMonAnFocusGained(evt);
            }
        });
        tfTenMonAn.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tfTenMonAnKeyTyped(evt);
            }
        });

        jLabel1.setText("Tên Món Ăn");

        jLabel3.setText("Từ:");

        jLabel2.setText("Đơn Giá");

        jLabel4.setText("Đến:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(11, 11, 11)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(tfDen, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfTu)
                    .addComponent(tfTenMonAn))
                .addGap(41, 41, 41)
                .addComponent(btTimMA)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(tfTenMonAn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfTu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(13, 13, 13)))
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tfDen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btTimMA)
                .addGap(52, 52, 52))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 673, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(87, 87, 87))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(109, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 854, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 4, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 4, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 624, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 14, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 15, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btTimMAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTimMAActionPerformed
        // TODO add your handling code here:
        tfTenMonAn.setText("");
        Float tu;
        Float den;
        try {
            tu = Float.parseFloat(tfTu.getText());
            if (tu < 0 || tu > 10000000) {
                JOptionPane.showMessageDialog(rootPane, "Giá từ phải là số từ 0 đến 10000000!");
                tfTu.setText("0");
                return;
            }
        } catch (NumberFormatException | HeadlessException e) {
            JOptionPane.showMessageDialog(rootPane, "Giá từ phải là số từ 0 đến 10000000!");
            tfTu.setText("0");
            return;
        }
        try {
            den = Float.parseFloat(tfDen.getText());
            if (den < 0 || den > 10000000) {
                JOptionPane.showMessageDialog(rootPane, "Giá đến phải là số từ 0 đến 10000000!");
                tfDen.setText("0");
                return;
            }
        } catch (NumberFormatException | HeadlessException e) {
            JOptionPane.showMessageDialog(rootPane, "Giá đến phải là số từ 0 đến 10000000!");
            tfDen.setText("0");
            return;
        }
        if (tu > den) {
            JOptionPane.showMessageDialog(rootPane, "Giá đến phải lớn hơn giá từ!");
            return;
        }
        if (den != 0) {
            try {
                tbModelMA = MonAn_BUS.layBangMonAnTheoDonGia(tu, den);
                tbMonAn.setModel(tbModelMA);
            } catch (Exception ex) {
                Logger.getLogger(JIFTimMonAn.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
        }
    }//GEN-LAST:event_btTimMAActionPerformed

    private void tfTenMonAnFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfTenMonAnFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_tfTenMonAnFocusGained

    private void tfTenMonAnKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfTenMonAnKeyTyped
        // TODO add your handling code here:
        String tenMA = tfTenMonAn.getText();
        try {
            if (!"".equals(tenMA)) {
                tbModelMA = MonAn_BUS.layBangMonAnTheoTen(tenMA);
                tbMonAn.setModel(tbModelMA);
            }
        } catch (Exception ex) {
            Logger.getLogger(JIFTimMonAn.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_tfTenMonAnKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btTimMA;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbMonAn;
    private javax.swing.JTextField tfDen;
    private javax.swing.JTextField tfTenMonAn;
    private javax.swing.JTextField tfTu;
    // End of variables declaration//GEN-END:variables
}
